const express = require('express');
const Router = express.Router(); // ==app
let query = require("../../db/mysql.js");

//获取所有商品 传入 kid 获取 某一类型的商品
Router.get("/allgoods", async (req, res) => {
    let data = {};
    let kid = req.query.kid;
    let sql;
    if (kid == 999) {
        sql = `select * from goodsinfo`;
    } else if (kid) {
        sql = `select * from goodsinfo where kid='${kid}'`;
    } else {
        sql = `select * from goodsinfo`;
    }
    try {
        let p = await query(sql);
        if (p.length) {
            let goodsList = [{ gid: 999 }]
            if (kid==999) {
                let i = 0;
                let check = true;
                while (i < 49) {
                    let num = Math.floor(Math.random() * p.length);
                    for (let i of goodsList) {
                        if (p[num].gid == i.gid) {
                            check = false;
                            break;
                        }
                        check = true;
                    }
                    if (check) {
                        goodsList.push(p[num]);
                        i++;
                    }
                }
                goodsList.splice(0, 1);
                // console.log(this.goodsList);
                let arrList = [];
                for (let i = 0; i < 7; i++) {
                    let arr = [];
                    for (let j = 7 * i; j < 7 * i + 7; j++) {
                        arr.push(goodsList[j]);
                    }
                    arrList.push(arr);
                }
                goodsList = arrList;
                data = {
                    code: 2000,
                    flag: true,
                    message: '获取成功',
                    total: goodsList.length,
                    data: goodsList
                }
            }else{
                data = {
                    code: 2000,
                    flag: true,
                    message: '获取成功',
                    total: p.length,
                    data: p
                }
            }
            
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})
//分页获取 商品
Router.get('/padinggoods', async (req, res) => {
    let { page, size } = req.query;
    page = page || 1;
    size = size || 5;
    let data = {};
    let index = (page - 1) * size;
    try {
        let sql = `select * from goodsinfo where gid limit ${index},${size}`;
        let sqltotal = `select * from goodsinfo`;
        let arr = await query(sqltotal);
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                total: arr.length,
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})
//获取某一商品的商品信息
Router.get("/goodinfo", async (req, res) => {
    let data = {};
    let gid = req.query.gid;
    try {
        let sql = `select * from goodsinfo where gid='${gid}'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})
//增加商品
Router.post("/addgood/:typeId/:kid", async (req, res) => {
    let data = {};
    let { typeId, kid } = req.params;
    let { gname, gimg, gprice, gstock } = req.body;
    try {
        let sql = `insert into goodsinfo(typeId,kid,gname,gimg,gprice,gstock) value('${typeId}','${kid}','${gname}','${gimg}','${gprice}','${gstock}')`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '增加成功'
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '增加失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data);
})
//删除商品
Router.delete("/delgood", async (req, res) => {
    let data = {};
    let { gid } = req.body;
    try {
        let sql = `delete from goodsinfo where gid='${gid}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '删除成功'
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '删除失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})
//修改商品信息
Router.put("/editgood/:gid", async (req, res) => {
    let data = {};
    let { gid } = req.params;
    let str = ``;
    for (let key in req.body) {
        str += key + '=' + `'${req.body[key]}'` + ','
    }
    str = str.slice(0, -1);
    try {
        let sql = `update goodsinfo set ${str} where gid='${gid}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '修改成功'
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})
//模糊搜索 商品
Router.get("/searchgood", async (req, res) => {
    let data = {};
    let { gname } = req.query;
    try {
        let sql = `select gid,gname from goodsinfo where gname like '%${gname}%'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '搜索成功',
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '搜索失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})
//搜索 商品
Router.get("/searchgoods", async (req, res) => {
    let data = {};
    let { gname, typeId, kid } = req.query;
    gname = gname || '';
    typeId = typeId || '';
    kid = kid || '';
    try {
        //gname,tid,kid 可传可不传
        let sql = `select * from goodsinfo where gname like '%${gname}%' and ( goodsinfo.typeId='${typeId}' or '${typeId}'='') and (goodsinfo.kid='${kid}' or '${kid}'='')`;
        // console.log(sql);
        let p = await query(sql);
        if (p) {
            data = {
                code: 2000,
                flag: true,
                message: '搜索成功',
                total: p.length,
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '搜索失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})
module.exports = Router;